<template>
	<view>
		<view class="new-version">
			当前最新版本是：{{latestVersion}}
			<button class="update" type="primary" size="mini" @click="update">点击更新</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				latestVersion: '',
				current_version:'1.2',
				httpv:'https://www.wujiaqi.xyz'
			}
		},
		
		created() {
			this.getLatestVersion();
		},
		
		methods: {
			async getLatestVersion() {
				let result = await this.$request({
					url: '/newtest/user-version/getversion'
				})
				
				// console.log('result = ', result)
				this.latestVersion = result.data.res
				return this.latestVersion
			},
			
			// 更新版本
			async update() {
				if (this.current_version == await this.getLatestVersion()) {
					uni.showToast({
						title: '当前已是最新版本',
						duration: 2000
					});
					return
				}
				
				uni.getSystemInfo({
					success:(res) => {
						//检测当前平台，如果是安卓则启动安卓更新  
						if(res.platform=="android"){  
							this.AndroidCheckUpdate();  
						}  
					}  
				})
			},
			
			AndroidCheckUpdate:function(){
				var _this = this;  
				uni.request({
					//请求地址，设置为自己的服务器链接 获取服务端版本号
					url: _this.httpv + '/supermarket/newtest/user-version/getversion',
					method: 'GET',  
					data: {},  
					success: res => {
						var server_version = res.data.data.res;
						_this.checkVersionToLoadUpdate(server_version,this.current_version);
						console.log('安卓服务器版本号是'+res.data.data.res)
						console.log(res.data.data.res)
					},  
					fail: () => {},  
					complete: () => {}  
				});  
			}, 
			checkVersionToLoadUpdate:function(server_version,curr_version){
				var _this = this;  
				if(server_version > curr_version){
					uni.showModal({
						title: "版本更新",
						content: '有新的版本发布，是否立即进行新版本下载？',
						confirmText:'立即更新',
						cancelText:'稍后进行',
						success: function (res) {
							if (res.confirm) {
								uni.showToast({
									icon:"none",
									mask: true,
									title: '有新的版本发布，程序已启动自动更新。新版本下载完成后将自动弹出安装程序',  
									duration: 5000,  
								}); 
								//设置 最新版本apk的下载链接
								var downloadApkUrl = _this.httpv + '/supermarket.apk';
								// var downloadApkUrl = 'https://service.dcloud.net.cn/build/download/71994300-f244-11ea-9722-0512b4d16649';
								var dtask = plus.downloader.createDownload( downloadApkUrl, {}, function ( d, status ) {  
										// 下载完成  
										if ( status == 200 ) {   
											plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename),{},{},function(error){  
												uni.showToast({  
													title: '安装失败', 
													duration: 1500  
												});  
											})
										} else {  
											 uni.showToast({  
												title: '更新失败',
												duration: 1500  
											 });  
										}    
									});  
								dtask.start();
							} else if (res.cancel) {
								console.log('稍后更新');
							}
						}
					});
				}
				
			},
			
		}
	}
</script>

<style lang="scss" scoped>
	.new-version{
		background-color: #FFFFFF;
		padding: 40rpx;
		position: relative;
	}
	.update{
		position: absolute;
		right: 0;
		top: 50%;
		transform: translate(-20%, -50%);
	}
</style>
